package com.changzhi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changzhi.common.constans.RedisConstans;
import com.changzhi.common.exception.ServiceException;
import com.changzhi.common.util.Result;
import com.changzhi.ldyl.model.ChatChannel;
import com.changzhi.ldyl.model.vo.UserVo;
import com.changzhi.service.ChatChannelService;
import com.changzhi.util.CommonUtil;
import com.changzhi.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:测试
 * @author: 小落
 * @date: 2020年10月10日
 */
@Slf4j
@RestController
@RequestMapping("cache")
public class CacheController {

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ChatChannelService chatChannelService;


	@PostMapping("get")
	public Object get(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		String key = (String) map.get("key");
		if(StringUtils.isBlank(key)){
			throw new ServiceException("key不能为空");
		}
		return redisUtil.get(key);
	}

	@PostMapping("hash")
	public Object hash(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		String key = (String) map.get("key");
		String item = (String) map.get("item");
		if(StringUtils.isBlank(key)){
			throw new ServiceException("key不能为空");
		}
		return redisUtil.hget(key, item);
	}

	@PostMapping("hashAll")
	public Object hashAll(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		String key = (String) map.get("key");
		if(StringUtils.isBlank(key)){
			throw new ServiceException("key不能为空");
		}
		return redisUtil.hGetValuesList(key);
	}


	@PostMapping("setHash")
	public Object setHash(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		String key = (String) map.get("key");
		String item = (String) map.get("item");
		String value = (String) map.get("value");
		if(StringUtils.isBlank(key)){
			throw new ServiceException("key不能为空");
		}
		if(StringUtils.isBlank(item)){
			throw new ServiceException("item不能为空");
		}
		UserVo userVo = (UserVo) redisUtil.hget(key, item);
		if(userVo != null){
			userVo.setValue(StringUtils.isBlank(value)?0:Integer.valueOf(value));
			redisUtil.hset(key,item,userVo,RedisConstans.ONE_MONTH);
		}
		return Result.success();
	}



	@PostMapping("delete")
	public Result delete(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		//
		//redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "0");
//		redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "1");
//		redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "2");
//		redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "3");
//		redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "4");
//		redisUtil.del(RedisConstans.YEAR_CHANNEL + "_" + "5");
		//
		//redisUtil.del(RedisConstans.YEAR_EXTERNAL);
		//
		//redisUtil.del(RedisConstans.YEAR_BOSS);
		//redisUtil.del(RedisConstans.YEAR_BOSS_VALUE);
		//
		//redisUtil.del(RedisConstans.YEAR_STAR + "_" + "0");
		//redisUtil.del(RedisConstans.YEAR_STAR + "_" + "1");
		//redisUtil.del(RedisConstans.YEAR_STAR + "_" + "2");
		//
		//redisUtil.del(RedisConstans.YEAR_CHANNEL_PK + "_" + "1");
		redisUtil.del(RedisConstans.YEAR_CHANNEL_PK + "_" + "2");
		redisUtil.del(RedisConstans.YEAR_CHANNEL_PK + "_" + "3");
		redisUtil.del(RedisConstans.YEAR_CHANNEL_PK + "_" + "4");
		redisUtil.del(RedisConstans.YEAR_CHANNEL_PK + "_" + "5");
		return Result.success();
	}

	private void checkPwd(String pwd){
		if(!"8704".equals(pwd)){
			throw new ServiceException("密码错误！");
		}
	}


	/**
	 * 查询value对应的score   zscore
	 * @return
	 */
	@PostMapping("setScore")
	public Double setScore(@RequestBody Map<String,Object> map) {
		CommonUtil.checkPwd((String) map.get("pwd"));
		String key = RedisConstans.YEAR_BOSS_VALUE;
		String item = (String) map.get("item");
		String score = (String) map.get("score");
		redisTemplate.opsForZSet().add(key, item, StringUtils.isBlank(score)?0L:Long.valueOf(score));
		return redisTemplate.opsForZSet().score(key, item);
	}

	private ChatChannel getChatChannelInfo(Integer accountId){
		QueryWrapper<ChatChannel> wrapper = new QueryWrapper<>();
		wrapper.lambda().eq(ChatChannel::getOwnerAccountid, accountId);
		wrapper.last("limit 1");
		return chatChannelService.getOne(wrapper);
	}
}
